package com.vrv.common.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;

import android.util.Log;

import com.vrv.common.GlobalConstants;
import com.vrv.netmgr.vo.Classes;

/**
 * @Description 连接服务器
 *
 * @author gaopei
 * @date
 */
public class HttpClientToServer {

    public static final String DO_POST_ERR = "error";
    public static final String DO_POST_EXCEPT = "exception";

    public String doGet(String username, String password) {

        String getUrl = "?username=" + username + "&password=" + password;
        HttpGet httpGet = new HttpGet(getUrl);
        HttpClient hc = new DefaultHttpClient();
        try {
            HttpResponse ht = hc.execute(httpGet);
            if (ht.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                HttpEntity he = ht.getEntity();
                InputStream is = he.getContent();
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                String response = "";
                String readLine = null;
                while ((readLine = br.readLine()) != null) {
                    response = response + readLine;
                }
                is.close();
                br.close();

                return response;
            } else {
                return "error";
            }
        } catch (ClientProtocolException e) {

            e.printStackTrace();
            return "exception";
        } catch (IOException e) {

            e.printStackTrace();
            return "exception";
        }

    }

    public String doPost(String urlAddress, Map<String, String> map) {
        HttpPost httpPost = new HttpPost(urlAddress);
        List<NameValuePair> params = new ArrayList<NameValuePair>();

        if (null != map && map.size() != 0) {
            Iterator<Entry<String, String>> iter = map.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry<String, String> entry = (Entry<String, String>) iter.next();
                String key = entry.getKey();
                String val = entry.getValue();
                NameValuePair nvp = new BasicNameValuePair(key, val);
                params.add(nvp);
            }
        }

        // 添加当前用户的相关信息
        // TODO xxx 目前为了方便统一添加的“系统区域”的参数 后续要改
        List<Classes> classes = new ArrayList<Classes>();
        if (null != GlobalConstants.userInfo && (classes = GlobalConstants.userInfo.getClasses()) != null && classes.size() > 0) {
            NameValuePair nvp = new BasicNameValuePair("classId", classes.get(0).getId());
            Log.d("#netmgr#", "{classId:" + classes.get(0).getId() + ",className:" + classes.get(0).getName() + "}");
            params.add(nvp);
        }

        try {
            HttpEntity he = new UrlEncodedFormEntity(params, "utf-8");
            httpPost.setEntity(he);
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }
        
        HttpClient hc = new DefaultHttpClient();
        //连接超时设置
        hc.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30 * 1000);
        //响应超时设置
        hc.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 30 * 1000);
        
        try {
            HttpResponse ht = hc.execute(httpPost);
            if (ht.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                HttpEntity het = ht.getEntity();
                InputStream is = het.getContent();
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                String response = "";
                String readLine = null;
                while ((readLine = br.readLine()) != null) {
                    response = response + readLine;
                }
                is.close();
                br.close();
                return response;
            } else {
                return DO_POST_ERR;
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            return DO_POST_EXCEPT;
        } catch (IOException e) {
            e.printStackTrace();
            return DO_POST_EXCEPT;
        }

    }

}
